import config from '../config/config.js'
import moment from 'moment'

const blogRegx = /^(.+)?\r\n\s*(.+)?\r\n/
const pngRegx = /\!\[.*\]\((.+)\)/

const itemColor = [
				    'rgb(249, 191, 69)', 'rgb(134, 193, 102)', 'rgb(185, 136, 125)', 'rgb(241, 148, 131)', 'rgb(178, 143, 206)',
				    'rgb(134, 120, 53)', 'rgb(46, 169, 223)', 'rgb(231, 148, 96)', 'rgb(245, 150, 170)'
				  ]
export const formatBlog = (blog) => {
  let res =  blogRegx.exec(blog.body);
  blog.desc = res[1]
  blog.createTime = moment(blog.created_at).format("YYYY-MM-DD hh:mm:ss")
  return blog;
};

export const formatBlogs = (blogs) => {
  blogs.forEach(blog => {
	let res =  blogRegx.exec(blog.body);
    let cover = pngRegx.exec(blog.body)
    blog.desc = res[1]
	if(cover!=null){		
      blog.cover = cover[1]
	}else{
	  blog.cover = config.cover
	}
    blog.createTime = moment(blog.created_at).format("YYYY-MM-DD")
  })
  return blogs;
};

export const formatInspiration = (inspirations) => {
	inspirations.forEach((inspiration,index) => {	  
	  inspiration.createTime = moment(inspiration.created_at).format("YYYY-MM-DD hh:mm:ss")
	  inspiration.color = itemColor[index]
	})
	return inspirations;
}
